import 'dart:io';
import 'dart:math';

import 'package:aliplayer_widget/aliplayer_widget_lib.dart';
import 'package:aliplayer_widget/ui/aliplayer_bottom_bar_widget.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gameplay_flutter/lib_baseview/theme/jr.dart';
import 'package:gameplay_flutter/m_discover/player/video_gift_grid.dart';
import 'package:get/get.dart';
import 'package:ones/ui.dart';
import '../../generated/assets.dart';
import '../../lib_basemodel/discover/comment_entity.dart';
import '../../lib_basemodel/discover/moment_entity.dart';
import '../../lib_baseview/widget/dialogs.dart';
import '../../lib_baseview/widget/web/common_web_view.dart';
import '../../lib_chat/chat/index.dart';
import '../../lib_im/index.dart';
import '../../lib_user/user_manager.dart';
import '../../m_me/user_details_page/custom_tab_bar_indicator.dart';
import '../../m_me/user_details_page/user_details_page.dart';
import '../../resource/language/generated/l10n.dart';
import '../post/post_view.dart';
import '../sel_friend/sel_friend_view.dart';
import 'player_logic.dart';

class PlayerPage extends StatelessWidget {
  PlayerPage({int? id, Key? key}) : super(key: key) {
    logic = Get.put(PlayerLogic(), tag: id?.toString());
  }

  late final PlayerLogic logic;

  @override
  Widget build(BuildContext context) {
    TextDirection currentDirection = Directionality.of(context);
    bool isRTL = currentDirection == TextDirection.rtl; // 判断是否为 RTL 布局
    return Obx(() {
      return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.black,
        body: PageView.builder(
          controller: logic.pageController,
          scrollDirection: Axis.vertical,
          itemCount: logic.dramas.length,
          // itemCount: 8,
          itemBuilder: (context, index) {
            var moment = logic.dramas[index];
            var controller = logic.getCurrentPlayController(index);
            return Obx(() {
              return Stack(
                children: [
                  Column(
                    children: [
                      Stack(
                        children: [
                          Center(
                            child: AliPlayerWidget(
                              controller,
                            ),
                          ),
                          Center().onClick(
                            () {
                              logic.showEmojiView(false);
                              Future.delayed(
                                100.milliseconds,
                                () {
                                  logic.showWhiteInput(false);
                                  logic.messageFocus.unfocus();
                                  logic.commentId = null;
                                  logic.hintText(T.current.discover3_say_something);
                                },
                              );
                            },
                          ).positioned(right: 0, left: 0, top: 0, bottom: 0),
                        ],
                      ).expanded(),
                      Visibility(
                        // visible: !logic.showWhiteInput(),
                        child: Row(
                          children: [
                            Text(T.current.discover3_say_something).color(Color(0xffA8A8A8)).expanded(),
                            Image.asset(Assets.discoverAt, width: 28, height: 28).marginRight(3),
                            Image.asset(Assets.discoverEmoji, width: 28, height: 28).marginRight(3).onClick(() {
                              logic.showWhiteInput(true);
                              logic.showEmojiView(true);
                              logic.showSendButton(true);
                            }),
                          ],
                        )
                            .paddingOnly(top: 6, bottom: 8, left: 13, right: 10)
                            .backgroundColorInt(0xff353535)
                            .roundedRect(40)
                            .onClick(() {
                              logic.showWhiteInput(true);
                              logic.getFocus();
                            })
                            .paddingSymmetric(horizontal: 15, vertical: 10)
                            .backgroundColorInt(0xff2B2B2B),
                      ),
                    ],
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(moment.userNickname)
                          .fontSize(22)
                          .fontWeight(FontWeight.w500)
                          .color(Colors.white.withValues(alpha: 0.9))
                          .marginBottom(6),
                      Text(moment.titleStr).color(Colors.white.withValues(alpha: 0.9)).marginBottom(11),
                    ],
                  ).positioned(bottom: 82, left: 22, right: 22),
                  Positioned(
                    right: isRTL ? null : 16,
                    left: isRTL ? 16 : null,
                    bottom: 82,
                    child: Column(
                      children: [
                        Stack(
                          children: [
                            JR
                                .img(moment.userHeadUrl, fit: BoxFit.cover, width: 52, height: 52)
                                .roundedRect(90)
                                .onClick(() => Get.to(() => UserDetailsPage(userId: moment.userId.toString())))
                                .marginBottom(10),
                            Visibility(
                              visible: UserManager.instance.id != moment.userId,
                              child: JR
                                  .img(moment.isAttention == 1 ? Assets.discoverVideoRight : Assets.discoverVideoFollow,
                                      matchTextDirection: true, width: 20, height: 20)
                                  .onClick(() {
                                logic.attention(moment);
                              }).positioned(bottom: 0, right: 0, left: 0),
                            ),
                          ],
                        ).marginBottom(28),
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            JR.img(moment.isLike ? Assets.discoverVideoStar : Assets.discoverVideoStarGray,
                                matchTextDirection: true, width: 40, height: 40),
                            Text(moment.likes.toString()).fontSize(12).fontWeight(FontWeight.w500).color(Colors.white)
                          ],
                        ).onClick(() {
                          Debouncer.run("like", 800.milliseconds, () {
                            logic.like(moment);
                          });
                        }).marginBottom(10),
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            JR.img(Assets.discoverVideoComment, width: 40, height: 40),
                            Text(T.current.discover3_video)
                                .fontSize(12)
                                .fontWeight(FontWeight.w500)
                                .color(Colors.white),
                          ],
                        ).onClick(() async {
                          await showCommentGiftBottomSheet();
                          logic.showEmojiView(false);
                          logic.lockMode(false);
                          logic.showWhiteInput(false);
                          // logic.clickComment();
                        }).marginBottom(10),
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            JR.img(Assets.discoverVideoGift, width: 40, height: 40),
                            Text(moment.rewards.toString())
                                .fontSize(12)
                                .fontWeight(FontWeight.w500)
                                .color(Colors.white),
                          ],
                        ).onClick(() {
                          showGiftBottomSheet();
                        }).marginBottom(10),
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            JR.img(Assets.discoverVideoShare, width: 40, height: 40),
                            Text(T.current.discover3_share)
                                .fontSize(12)
                                .fontWeight(FontWeight.w500)
                                .color(Colors.white),
                          ],
                        ).onClick(() async {
                          var user = await Get.to(() => SelFriendPage(), arguments: true);
                          if (user is FriendModel) {
                            debugPrint("发送分享消息： ${user}");
                            ImManager.of.sendShareMessage(
                              targetId: user.imId?.toString() ?? "",
                              id: logic.currentDrama().id.toString(),
                              type: logic.currentDrama().type,
                              image: logic.currentDrama().cover ?? "",
                              title: logic.currentDrama().titleStr,
                              avatar: user.headPortraitsUrl ?? "",
                              nickname: user.nickname ?? "",
                            );
                            showToast(T.current.discover9_share_success);
                          }
                        }).marginBottom(10),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: logic.showWhiteInput() && !(Get.isBottomSheetOpen ?? false),
                    child: inputView(context, logic.currentDrama()),
                  ).positioned(left: 0, right: 0, bottom: logic.keyboardHeight()),
                  SizedBox(child: JR.img(Assets.imagesBackArrow, width: 10, height: 20))
                      .paddingSymmetric(horizontal: 22, vertical: 16)
                      .onClick(
                    () {
                      Get.back();
                    },
                  ),
                ],
              ).onClick(
                () {
                  logic.showWhiteInput(false);
                  logic.showEmojiView(false);
                  logic.messageFocus.unfocus();
                  logic.commentId = null;
                  logic.hintText(T.current.discover3_say_something);
                },
              ).paddingTop(Get.statusBarHeight / Get.pixelRatio);
            });
          },
        ),
      );
    });
  }

  /// 构建底部栏控件
// Widget _buildBottomBarWidget() {
//   var _playController = logic.currentController;
//   // 拖动控制
//   // bool enableDrag = !_isSceneLive();
//   bool enableDrag = true;
//   // seek 控制
//   bool enableSeek = true;
//
//   // 监听播放状态、当前播放位置、缓冲位置、总时长等
//   Listenable listenable = Listenable.merge([
//     _playController.playStateNotifier,
//     _playController.currentPositionNotifier,
//     _playController.bufferedPositionNotifier,
//     _playController.totalDurationNotifier,
//   ]);
//   return ListenableBuilder(
//     listenable: listenable,
//     builder: (context, _) {
//       // 获取播放状态
//       final playState = _playController.playStateNotifier.value;
//       // 获取当前播放位置、缓冲位置、总时长
//       final currentPosition = _playController.currentPositionNotifier.value;
//       final bufferedPosition = _playController.bufferedPositionNotifier.value;
//       final totalDuration = _playController.totalDurationNotifier.value;
//       return Positioned(
//         bottom: 0,
//         left: 0,
//         right: 0,
//         child: AliPlayerBottomBarWidget(
//           animationManager: _animationManager,
//           isPlaying: playState == FlutterAvpdef.started,
//           currentPosition: currentPosition,
//           bufferedPosition: bufferedPosition,
//           totalDuration: totalDuration,
//           onPlayIconPressed: _onPlayerViewTap,
//           onFullScreenPressed: _onFullScreenPressed,
//           onDragUpdate: enableDrag ? _onDragUpdate : null,
//           onDragEnd: enableDrag ? _onDragEnd : null,
//           onSeekEnd: enableSeek ? _onSeekEnd : null,
//         ),
//       );
//     },
//   );
// }

  showCommentGiftBottomSheet() async {
    logic.discoverCommentList();
    logic.discoverDynamicFind();
    logic.dynamicGifts();
    return Get.bottomSheet(
      Obx(() {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 10),
            TabBar(
              controller: logic.tabController,
              onTap: (value) {
                if (logic.lockMode() && logic.tabController.indexIsChanging) {
                  logic.tabController.animateTo(logic.tabController.previousIndex);
                }
              },
              indicator: ImageTabIndicator(image: AssetImage(Assets.discoverIndicate1), width: 15, height: 6),
              dividerColor: Colors.transparent,
              labelStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: Color(0xff1B1B1B)),
              unselectedLabelStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: Color(0xffc1c1c1)),
              tabs: [
                Tab(text: T.current.discover3_video),
                Tab(text: T.current.discover7_gifts),
              ],
            ),
            SizedBox(height: 10),
            TabBarView(
                controller: logic.tabController,
                physics: logic.lockMode() ? NeverScrollableScrollPhysics() : null,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Visibility(
                        visible: logic.comments.isNotEmpty,
                        replacement: Center(
                            child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            JR.img(Assets.discoverNoComment, width: 50, height: 50),
                            Text(T.current.discover7_no_comments_yet).fontSize(11).colorInt(0xffD8D8D8).marginTop(13),
                          ],
                        )).expanded(),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text("${logic.currentDrama().comments} ${T.current.discover3_video}")
                                .colorInt(0xffA8A8A8)
                                .fontSize(14)
                                .marginSymmetric(horizontal: 16),
                            SizedBox(height: 8),
                            ListView.builder(
                              padding: EdgeInsets.zero,
                              shrinkWrap: true,
                              itemCount: logic.comments.length,
                              itemBuilder: (context, index) {
                                var comment = logic.comments[index];
                                return commentView(comment);
                              },
                            ).paddingSymmetric(horizontal: 16).expanded(),
                          ],
                        ).expanded(),
                      ),
                      inputView(Get.context!, logic.currentDrama()),
                    ],
                  ),
                  Visibility(
                    visible: logic.giftList.isNotEmpty,
                    replacement: Center(
                        child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        JR.img(Assets.discoverNoGift, width: 50, height: 50),
                        // Text(T.current.gift_gift).fontSize(11).colorInt(0xffD8D8D8).marginTop(13),
                        Text(T.current.discover7_no_comments_yet).fontSize(11).colorInt(0xffD8D8D8).marginTop(13),
                      ],
                    )),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text("${logic.giftDto().total} ${T.current.discover9_gift_value}")
                            .colorInt(0xffA8A8A8)
                            .fontSize(14)
                            .marginSymmetric(horizontal: 16),
                        SizedBox(height: 8),
                        ListView.builder(
                          padding: EdgeInsets.zero,
                          shrinkWrap: true,
                          itemCount: logic.giftList.length,
                          itemBuilder: (context, index) {
                            var giftRecord = logic.giftList[index];
                            return Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                JR
                                    .img(giftRecord.fromUserHeadUrl, fit: BoxFit.cover, width: 32, height: 32)
                                    .roundedRect(90)
                                    .onClick(
                                        () => Get.to(() => UserDetailsPage(userId: giftRecord.fromUserId.toString()))),
                                SizedBox(width: 8),
                                Text(giftRecord.fromUserName).colorInt(0xff1B1B1B).fontSize(18),
                                Spacer(),
                                JR.img(giftRecord.objectUrl, width: 32, height: 32),
                                SizedBox(width: 2),
                                JR.img(Assets.discoverCommentClose, width: 10, height: 10),
                                SizedBox(width: 2),
                                Text(giftRecord.amount.toString()).colorInt(0xffA8A8A8),
                              ],
                            ).paddingSymmetric(vertical: 12);
                          },
                        ).paddingSymmetric(horizontal: 16).expanded(),
                      ],
                    ),
                  ),
                ]).expanded(),
          ],
        ).paddingBottom(Get.bottomBarHeight / Get.pixelRatio).backgroundColor(Colors.white).roundedRectVertical(12, 0);
      }),
    );
  }

  showGiftBottomSheet() async {
    Get.bottomSheet(
      Obx(() {
        return Stack(
          children: [
            Column(
              children: [
                Row(
                  children: [
                    Image.asset(Images.icChatDiamond, width: 16, height: 16).marginRight(3),
                    Text(logic.balance().toString()).color(Colors.white),
                    Icon(Icons.arrow_forward_ios, size: 10, color: Colors.white),
                  ],
                ).paddingSymmetric(horizontal: 15).onClick(() {
                  CommonWebView.show(Get.context!,
                      "https://h5web.yahabbi.com/wallet/index.html?hideNavi=1&hideBottomSafeArea=1&is_full_screen=1");
                }).marginTop(18),
                TabBar(
                  controller: logic.giftTabController,
                  isScrollable: true,
                  physics: const ClampingScrollPhysics(),
                  dividerColor: Colors.transparent,
                  dividerHeight: 0,
                  tabAlignment: TabAlignment.start,
                  indicatorSize: TabBarIndicatorSize.label,
                  indicatorWeight: 1,
                  indicatorPadding: const EdgeInsets.only(bottom: 6),
                  indicator: ImageTabIndicator(image: AssetImage(Assets.discoverWhiteIndicator), width: 15, height: 6),
                  unselectedLabelStyle: const TextStyle(color: Color(0xFFA397DE), fontSize: 16),
                  labelStyle: const TextStyle(
                    color: Color(0xFFFFFFFF),
                    fontWeight: FontWeight.w700,
                    fontSize: 16,
                  ),
                  tabs: logic.wrappers.map((e) => Tab(text: e.name)).toList(),
                ),
                Expanded(
                  child: SafeArea(
                    child: TabBarView(
                      controller: logic.giftTabController,
                      children:
                          logic.wrappers.map((e) => VideoGiftGrid(e, logic: logic, gifts: logic.getGifts(e))).toList(),
                    ),
                  ),
                ),
              ],
            ).marginBottom(Platform.isIOS ? 60 : 80),
            Positioned(
              bottom: 28,
              left: 0,
              right: 0,
              child: Column(
                children: [
                  Container(height: 0.5, color: Color(0xff5B5B5B)).marginBottom(9),
                  Row(
                    children: [
                      SizedBox(width: 5),
                      Row(
                        children: [
                          JR
                              .img(logic.currentDrama().userHeadUrl, fit: BoxFit.cover, width: 26, height: 26)
                              .roundedRect(90),
                          SizedBox(width: 8),
                          Text(logic.currentDrama().userNickname).singleLine().fontSize(16).color(Colors.white),
                        ],
                      )
                          .paddingAll(6)
                          .backgroundColor(Colors.black12)
                          .width(180)
                          .borderIs(color: Colors.white54, radius: 90),

                      // _Recharge(onTap: () {}),
                      const Spacer(),
                      _Send(logic),
                      SizedBox(width: 5),
                    ],
                  ).paddingBottom(0),
                ],
              ),
            )
          ],
        ).backgroundColorInt(0xff1C1424).roundedRectVertical(18, 0);
      }),
    );
  }

  Widget inputView(BuildContext context, MomentEntity moment) {
    // print(" MomentEntity moment  ${moment.isLike}");
    // print(" MomentEntity moment  ${moment}");

    TextDirection currentDirection = Directionality.of(context);
    bool isRTL = currentDirection == TextDirection.rtl; // 判断是否为 RTL 布局

    return Obx(() {
      return Column(
        children: [
          Row(
            children: [
              Row(
                children: [
                  Directionality(
                    textDirection: isRTL ? TextDirection.rtl : TextDirection.ltr,
                    child: TextField(
                      textDirection: TextDirection.ltr,
                      textAlign: isRTL ? TextAlign.end : TextAlign.start,
                      controller: logic.messageController,
                      focusNode: logic.messageFocus,
                      onTap: () {
                        logic.hintText(T.current.discover3_say_something);
                        logic.showEmojiView(false);
                        logic.commentId = null;
                      },
                      onChanged: (value) {
                        if (value.isNotEmpty) {
                          logic.sendEnable(true);
                        } else {
                          logic.sendEnable(false);
                        }
                        if (value.isNotEmpty && value.length > logic.previewText.length && value.endsWith(' @')) {
                          logic.atUser();
                        }
                        logic.previewText = value;
                      },
                      onSubmitted: (value) {
                        logic.discoverComment();
                      },
                      // maxLines: 2,
                      style: const TextStyle(color: Color(0xff1B1B1B), fontSize: 14),
                      decoration: InputDecoration(
                          hintText: logic.hintText(),
                          hintStyle: const TextStyle(color: Color(0xffd8d8d8), fontSize: 14),
                          hintTextDirection: TextDirection.ltr,
                          border: InputBorder.none,
                          isCollapsed: true,
                          contentPadding: const EdgeInsets.symmetric(vertical: 7)),
                    ).expanded(),
                  ),
                  Row(
                    children: [
                      Image.asset(Assets.discoverAt, width: 28, height: 28).onClick(() {
                        logic.atUser();
                      }).marginRight(3),
                      Image.asset(Assets.discoverEmoji, width: 28, height: 28).onClick(
                        () {
                          // if (!logic.showEmojiView()) {
                          logic.messageFocus.unfocus();
                          // }
                          logic.showEmojiView.toggle();

                          if (logic.showEmojiView()) {
                            logic.lockMode(true);
                          }

                          if (!logic.showEmojiView() && !(Get.isBottomSheetOpen ?? false)) {
                            logic.showWhiteInput(false);
                          }
                        },
                      ).marginRight(3),
                    ],
                  ),
                ],
              ).paddingSymmetric(horizontal: 13).backgroundColorInt(0xffF2F2F2).roundedRect(90).expanded(),
              SizedBox(width: 12),
              Visibility(
                visible: !logic.showSendButton(),
                replacement: Opacity(
                  opacity: logic.sendEnable() ? 1 : 0.4,
                  child: Row(
                    children: [
                      Image.asset(Assets.discoverPost, width: 18, height: 18).marginRight(4),
                      Text(T.current.c_send).fontSize(16).colorInt(0xffffffff).fontWeight(FontWeight.w600),
                    ],
                  )
                      .paddingSymmetric(horizontal: 15, vertical: 7)
                      .backgroundColorInt(0xff8A72FE)
                      .roundedRect(90)
                      .onClick(() {
                    logic.discoverComment();
                  }),
                ),
                child: Row(
                  children: [
                    Column(
                      children: [
                        Image.asset(moment.isLike ? Assets.discoverStar : Assets.discoverStarGrey,
                                width: 32, height: 32)
                            .onClick(() {
                          logic.like(moment);
                        }),
                        Text(max(moment.likes, 0).toString())
                            .colorInt(0xff444444)
                            .fontSize(14)
                            .fontWeight(FontWeight.w500),
                      ],
                    ).marginRight(9),
                    Column(
                      children: [
                        Image.asset(Assets.discoverComment, width: 32, height: 32),
                        Text(moment.comments.toString()).colorInt(0xff444444).fontSize(14).fontWeight(FontWeight.w500),
                      ],
                    ).onClick(
                      () {
                        logic.clickComment();
                      },
                    ),
                  ],
                ),
              ),
            ],
          ).paddingSymmetric(horizontal: 16, vertical: 5),
          Visibility(
            visible: logic.showEmojiView(),
            child: MessageEmoji(
              onTap: (emoji) => logic.messageController.text += emoji,
            ).height(320),
          )
        ],
      );
    }).backgroundColor(Colors.white);
  }

  Column commentView(CommentEntity comment, {bool showDivider = true, bool isReply = false}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Material(
          color: Colors.transparent,
          shadowColor: Colors.transparent,
          child: InkWell(
            onTap: () {
              logic.showEmojiView(false);
              // logic.hintText("回复 @${comment.fromUserNickname}：");
              logic.commentId = comment.id;
              logic.messageFocus.requestFocus();
            },
            onLongPress: () {
              if (comment.fromUserId == UserManager.instance.id) {
                Get.bottomSheet(
                  Column(
                    children: [
                      Text(T.current.delete)
                          .color(Colors.black)
                          .fontSize(20)
                          .fontWeight(FontWeight.w600)
                          .paddingSymmetric(vertical: 18)
                          .onClick(() {
                        Get.back();
                        logic.deleteComment(comment.id);
                      }),
                      Container(height: 10, color: const Color(0xffFAFAFA)),
                      Text(T.current.cancel)
                          .color(const Color(0xffA8A8A8))
                          .fontSize(20)
                          .paddingSymmetric(vertical: 18)
                          .onClick(() {
                        Get.back();
                      }),
                    ],
                  )
                      .backgroundColor(Colors.white)
                      .roundedRectVertical(18, 0)
                      .height(120 + Get.bottomBarHeight / Get.pixelRatio),
                );
              }
            },
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            focusColor: Colors.transparent,
            hoverColor: Colors.transparent,
            enableFeedback: false,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    JR
                        .img(comment.fromUserHeadUrl, fit: BoxFit.cover, width: 24, height: 24)
                        .roundedRect(90)
                        .onClick(() => Get.to(() => UserDetailsPage(userId: comment.fromUserId.toString()))),
                    SizedBox(width: 6),
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(comment.fromUserNickname).colorInt(0xffC1C1C1).fontSize(12).marginBottom(1),
                        Row(
                          children: [
                            Text.rich(TextSpan(
                              children: [
                                if (comment.isReply == 1)
                                  TextSpan(
                                    text: "${T.current.discover9_reply} ",
                                    style: TextStyle(color: Color(0xff8A72FE), fontSize: 13),
                                  ),
                                if (comment.isReply == 1)
                                  TextSpan(
                                    text: comment.toUserNickname + ": ",
                                    style: const TextStyle(color: Color(0xffC1C1C1), fontSize: 13),
                                  ),
                                ...comment.contentInfoList.map(
                                  (e) {
                                    if (e.type == 1) {
                                      return TextSpan(
                                        text: "@${e.text}",
                                        style: const TextStyle(color: Color(0xff8A72FE), fontSize: 13),
                                      );
                                    } else {
                                      return TextSpan(
                                        text: e.text,
                                        style: const TextStyle(fontSize: 13),
                                      );
                                    }
                                  },
                                ),
                              ],
                            )).expanded(),
                          ],
                        ),
                        Text(comment.timeStr).colorInt(0xffC1C1C1).fontSize(10).marginBottom(1),
                      ],
                    ).marginRight(4).expanded(),
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Image.asset(comment.isLike == 1 ? Assets.discoverStar : Assets.discoverStarGrey,
                                width: 24, height: 24)
                            .marginBottom(1),
                        Text(comment.likes.toString()).colorInt(0xff444444).fontSize(10),
                      ],
                    ).onClick(() {
                      Debouncer.run("likeComment", 800.milliseconds, () {
                        logic.likeComment(comment);
                      });
                    }).marginRight(20),
                  ],
                ),
                const SizedBox(height: 4),
                Visibility(visible: showDivider, child: Container(height: 1, color: const Color(0xffF2F2F2))),
              ],
            ).padding(top: 8),
          ),
        ),
        if (comment.showMode)
          ...comment.replyList.map(
            (e) => commentView(e, showDivider: false, isReply: true).marginLeft(24),
          ),
        if (!isReply && !comment.showMode)
          ...comment.replyList.sublist(0, min(1, comment.replyList.length)).map(
                (e) => commentView(e, showDivider: false, isReply: true).marginLeft(24),
              ),
        if (!isReply && !comment.showMode && comment.replyList.length > 1)
          Text(T.current.discover9_show_more_replies)
              .colorInt(0xffA397DE)
              .fontSize(10)
              .marginLeft(24)
              .marginSymmetric(vertical: 8)
              .onClick(() {
            comment.showMode = true;
            logic.comments.refresh();
          }),
      ],
    );
  }
}

class _Send extends StatefulWidget {
  final PlayerLogic logic;

  const _Send(this.logic);

  @override
  State<_Send> createState() => __SendState();
}

class __SendState extends State<_Send> {
  final _numbers = [1, 7, 17, 77, 777];

  void _onSendGift() {
    // logic.sendGiftMessage();
    final objUrl = widget.logic.gift?.objectGifUrl;
    if (objUrl != null) widget.logic.showGift(Get.context!, objUrl);
  }

  Widget _counter(int value) {
    final last = value == _numbers.last;
    return Obx(() {
      return GestureDetector(
        onTap: () {
          Navigator.of(context).pop();
          widget.logic.setGiftCount(value);
          setState(() {});
        },
        behavior: HitTestBehavior.opaque,
        child: Container(
          width: 50,
          padding: const EdgeInsets.symmetric(vertical: 10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: const Color(0xFFF2F2F2),
            border: Border(
              bottom: BorderSide(color: last ? Colors.transparent : Colors.white),
            ),
          ),
          child: Text(
            '$value',
            style: TextStyle(
                fontWeight: FontWeight.w500,
                color: widget.logic.giftCount.value == value ? const Color(0xFF333333) : const Color(0xFFC1C1C1)),
          ),
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        CustomPopup(
          showArrow: false,
          contentPadding: EdgeInsets.zero,
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: _numbers.map(_counter).toList(),
          ).clipRRect(5),
          barrierColor: Colors.transparent,
          contentDecoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: const Color(0xFFF2F2F2),
          ),
          child: Container(
            padding: const EdgeInsets.fromLTRB(10, 4, 4, 4),
            constraints: const BoxConstraints(minWidth: 60),
            alignment: Alignment.center,
            child: Row(
              children: [
                Text(
                  '${widget.logic.giftCount()}',
                  style: const TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                    fontSize: 18,
                  ),
                ),
                const Icon(Icons.keyboard_arrow_up, size: 15, color: Colors.white),
              ],
            ),
          ),
        ),
        GestureDetector(
          onTap: () => _onSendGift(),
          behavior: HitTestBehavior.opaque,
          child: Container(
            padding: const EdgeInsets.fromLTRB(6, 8, 8, 4),
            height: 36,
            decoration: const BoxDecoration(
              color: Colors.green,
              // borderRadius: BorderRadius.horizontal(right: Radius.circular(99)),
              gradient: LinearGradient(
                colors: [Color(0xFF8A72FE), Color(0xFF6549EC)],
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
              ),
            ),
            child: Text(
              T.current.c_send.toUpperCase(),
              style: const TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                fontSize: 14,
              ),
            ),
          ),
        ),
      ],
    ).roundedRect(90).borderIs(color: Colors.white54, radius: 99);
  }
}
